<?php

namespace App\Http\Controllers\Mobile;

use App\Models\Product;
use App\Models\ProductImg;
use App\Repos\FavoriteRepo;
use App\Repos\ProductRepo;
use App\Repos\UserRepo;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ItemController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $wechatUser =  session('wechat.oauth_user.default');
        $openId = 0;
        if($wechatUser){
            $original   =  $wechatUser->original;
            if(!empty($original)){
                $openId     =  array_get($original,'openid','');
            }
        }

        $isFavorite =  FavoriteRepo::isFavorite($openId,$id);
        $userId = UserRepo::findByOpenid($openId);

        if(!$isFavorite->isEmpty()){
            $isFavorite = $isFavorite->first()->toArray();
        }else{
            $isFavorite['is_favorite'] = 0;
        }

        $where['status']  = 1;
        $where['id']      = (int)$id;
        $data = ProductRepo::find($where);
        //dd($data,$where);
        $data['specs'] = ProductRepo::getSpecs($where['id'],$data->type_id);
        $data['imgs']  = ProductRepo::imgs($where['id']);

        //规格
        $specs = array_get($data,'specs');

        $spec = [];

        foreach ($specs as $k=>$v){
            $spec[$k]['name'] = $v['name'];
            if(!empty($v['values'])){
                $spec[$k]['value']= [];
                foreach($v['values'] as $m =>$n){
                    array_push($spec[$k]['value'],$n['spec_name']);
                }
            }
        }
                	
        return view('mobile.item',compact('data','spec','openId','isFavorite','userId'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


    /**
     *  加收藏
     * @param Request $request
     * @return mixed
     */
    public function favorite(Request $request)
    {
        return FavoriteRepo::changeFavoriteState($request);
    }
}
